package com.qingfeng.core.dao.qingfeng;

import com.qingfeng.core.config.database.BaseMapper;
import com.qingfeng.core.dao.BaseDao;
import com.qingfeng.core.entity.qingfeng.Order;
import com.qingfeng.core.mapper.qingfeng.OrderMapper;
import com.qingfeng.core.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

/**
 * @description: 任务
 * @author:LWY
 * @date: 2017/10/16 17:15
 * @modified:
 */

@Repository
public class OrderDao extends BaseDao<Order> {

    @Autowired
    OrderMapper orderMapper;

    @Override
    protected BaseMapper<Order> getBaseMapper() {
        return orderMapper;
    }


    /**
     * 创建一个订单
     *
     * @param module_id 任务id
     * @param amount    金额
     * @param pay_way   支付方式
     * @return
     */
    public Order create(int type, String module_id, BigDecimal amount, Integer pay_way) {
        //生成订单号
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            sb.append(new Random().nextInt(10));
        }
        String id = DateUtil.format(new Date(), "yyyyMMddHHmmssffff") + sb;
        Order order = new Order();
        order.setId(id);
        order.setType(type);
        order.setModule_id(module_id);
        order.setAmount(amount);
        order.setPay_way(pay_way);
        order.setState(1);
        order.setCreate_time(new Date());
        order.setPay_time(new Date());
        orderMapper.insertSelective(order);
        return order;
    }
}
